package canss.webservice.controls;

import org.apache.beehive.controls.api.bean.Control;
import org.apache.beehive.controls.api.bean.ControlImplementation;

import weblogic.jdbc.SQLExceptionWrapper;

import canss.encoder.DBStringEncoder;
import canss.webservice.model.SCPContent;

import java.io.Serializable;

@ControlImplementation
public class SimplifiedContentControlImpl implements SimplifiedContentControl,
		Serializable
{
	private static final long serialVersionUID = -1090311972965580917L;
	
	@Control
	private BusinessSystemDB businesssystemdb;
	
	public SCPContent[] getSimplifiedContentsByCID(String _cid) throws SQLExceptionWrapper
	{
		return businesssystemdb.getSimplifiedContentsByCID(_cid);
	}

	
	private SCPContent fillupblanks(String _cid, SCPContent _scp) throws SQLExceptionWrapper
	{
		SCPContent[] list = getSimplifiedContentsByCID(_cid);
		if(list!=null && list.length>0)
		{
			SCPContent tmp = list[0];
			if(SCPContent.emptySCPContent.getDC_DATE()==_scp.getDC_DATE())
				_scp.setDC_DATE(tmp.getDC_DATE());
			
			if(SCPContent.emptySCPContent.getDC_DESCRIPTION().equals(_scp.getDC_DESCRIPTION()))
				_scp.setDC_DESCRIPTION(tmp.getDC_DESCRIPTION());
			
			if(SCPContent.emptySCPContent.getDC_PUBLISHER().equals(_scp.getDC_PUBLISHER()))
				_scp.setDC_PUBLISHER(tmp.getDC_PUBLISHER());
			
			if(SCPContent.emptySCPContent.getDC_SUBJECT().equals(_scp.getDC_SUBJECT()))
				_scp.setDC_SUBJECT(tmp.getDC_SUBJECT());
			
			if(SCPContent.emptySCPContent.getDC_TITLE().equals(_scp.getDC_TITLE()))
				_scp.setDC_TITLE(tmp.getDC_TITLE());
			
			if(SCPContent.emptySCPContent.getXMPDM_GENRE().equals(_scp.getXMPDM_GENRE()))
				_scp.setXMPDM_GENRE(tmp.getXMPDM_GENRE());
		}
		return _scp;
	}
		
	public Integer updateSimplifiedContentsByCID(String _cid, SCPContent _scpcontent) throws SQLExceptionWrapper
	{
		SCPContent _tmp_scpcontent = fillupblanks(_cid, _scpcontent);
		return businesssystemdb.updateSimplifiedContentsByCID(
				_cid, 
				_tmp_scpcontent.getDC_PUBLISHER(), 
				_tmp_scpcontent.getXMPDM_GENRE(), 
				_tmp_scpcontent.getDC_TITLE(), 
				_tmp_scpcontent.getDC_DESCRIPTION(), 
				_tmp_scpcontent.getDC_DATE(), 
				DBStringEncoder.getServerTime(), 
				_tmp_scpcontent.getDC_SUBJECT());
	}
}